package com.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pinda.dao.UUIDToDigits;
import com.pinda.feign.PdAreaControllerFeignClients;
//import com.pinda.feign.PdOrderCargoControllerFeignClients;
import com.pinda.feign.PdOrderControllerFeignClients;
import com.pinda.feign.PdTaskPickupDispatchControllerFeignClients;
import com.pinda.mapper.PdAddressBookMapper;
import com.pinda.oms.entity.PdOrder;
import com.pinda.oms.entity.PdOrderLocation;
import com.pinda.oms.entity.vo.Order_OrderCargoVo;
import com.pinda.users.entity.PdAddressBook;

import com.pinda.service.IPdAddressBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pinda.work.entity.PdTaskPickupDispatch;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 地址簿 服务实现类
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@Service
public class PdAddressBookServiceImpl extends ServiceImpl<PdAddressBookMapper, PdAddressBook> implements IPdAddressBookService {

    @Resource
    private PdAddressBookMapper pdAddressBookMapper;

    @Resource
    private PdAreaControllerFeignClients pdAreaControllerFeignClients;

    @Resource
    private PdOrderControllerFeignClients pdOrderControllerFeignClients;

    @Resource
    private PdTaskPickupDispatchControllerFeignClients pdTaskPickupDispatchControllerFeignClients;

//    @Resource
//    private PdOrderCargoControllerFeignClients pdOrderCargoControllerFeignClients;

    @Override
    public List<PdAddressBook> getByUserId(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",id);

        return this.pdAddressBookMapper.selectList(queryWrapper);
    }

    @Override
    public boolean saveAddressBook(PdAddressBook pdAddressBook) {
        String format = "YYYY-MM-dd hh:mm:ss";
        // DateTimeFormatter.ofPattern方法根据指定的格式输出时间
        String formatDateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern(format));
        LocalDateTime.now().format(DateTimeFormatter.ofPattern(format));
        // 当前时间
        pdAddressBook.setCreateTime(formatDateTime);

        PdAddressBook byCode = this.pdAreaControllerFeignClients.getByCode(pdAddressBook);

        byCode.setId(String.valueOf(UUID.randomUUID()));
        int insert = this.pdAddressBookMapper.insert(byCode);
        return insert!=0;
    }

    @Override
    public boolean updateAddressBook(PdAddressBook pdAddressBook) {

        PdAddressBook byCode = this.pdAreaControllerFeignClients.getByCode(pdAddressBook);
//        byCode.setId(String.valueOf(UUID.randomUUID()));
        int i = this.pdAddressBookMapper.updateById(pdAddressBook);

        return i!=0;
    }

    @Override
    public boolean saveOrder(Order_OrderCargoVo orderOrderCargoVo) throws ParseException {
        String format = "YYYY-MM-dd hh:mm:ss";
        // DateTimeFormatter.ofPattern方法根据指定的格式输出时间
        String formatDateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern(format));
        LocalDateTime.now().format(DateTimeFormatter.ofPattern(format));
        // 当前时间
        orderOrderCargoVo.getPdOrder().setCreateTime(formatDateTime);

        UUIDToDigits uuidToDigits = new UUIDToDigits();
        orderOrderCargoVo.getPdOrder().setId("D"+uuidToDigits.uuidToDigits());

        if(orderOrderCargoVo.getPdOrder().getReceiverProvinceId()==orderOrderCargoVo.getPdOrder().getSenderProvinceId()){
            orderOrderCargoVo.getPdOrder().setOrderType(1);
        }else{
            orderOrderCargoVo.getPdOrder().setOrderType(2);
        }


        PdOrder s = this.pdOrderControllerFeignClients.saveOrder(orderOrderCargoVo.getPdOrder());
//        System.out.println(s);

        orderOrderCargoVo.getPdOrderCargo().setOrderId(s.getId());
        orderOrderCargoVo.getPdOrderCargo().setId("C"+uuidToDigits.uuidToDigits());


        this.pdOrderControllerFeignClients.saveOrderLocation(orderOrderCargoVo.getPdOrder());

        PdTaskPickupDispatch pdTaskPickupDispatch = new PdTaskPickupDispatch();
        UUIDToDigits ff = new UUIDToDigits();
        pdTaskPickupDispatch.setId("Y"+ff.uuidToDigits());
        pdTaskPickupDispatch.setOrderId(s.getId());
        pdTaskPickupDispatch.setTaskType(1);
        pdTaskPickupDispatch.setStatus(1);

        String StringDate=s.getEstimatedArrivalTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date1 = sdf.parse(StringDate);
        Date date2 = sdf.parse(StringDate);
        long time1 = date1.getTime();
        long time2 = date2.getTime();
        //Date内部维护了一个long值，
        // 这个long值是表示从1970年到目前为止的毫秒数
//        System.out.println(time);
        //我们可以对这个毫秒值进行运行维护，比如说在这个基础上加上一天。
        time1 -= 3600000;
        time2 += 3600000;
        //然后对我们处理后的long重新设置
        date1.setTime(time1);
        date2.setTime(time2);
        String format1 = sdf.format(date1);
        String format2 = sdf.format(date2);
//        System.out.println(format);
        pdTaskPickupDispatch.setEstimatedStartTime(format1);
        pdTaskPickupDispatch.setEstimatedEndTime(format2);
        pdTaskPickupDispatch.setAssignedStatus(1);
        pdTaskPickupDispatch.setMark(orderOrderCargoVo.getPdOrderCargo().getRemark());
        pdTaskPickupDispatch.setCreateTime(s.getCreateTime());


        this.pdOrderControllerFeignClients.saveOrderCargo(orderOrderCargoVo.getPdOrderCargo());

        return this.pdTaskPickupDispatchControllerFeignClients.savePdTaskPickupDispatch(pdTaskPickupDispatch);
//        return false;
    }
}
